#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

const int N = 100005, bit = 20;
int n, m, dep[N], f[bit][N], t[N], sum[N], q[N], ans[N];
int e_cnt, heads[N];
struct Query {
  int typ, id;
  Query(int _t, int _id) : typ(_t), id(_id) {}
};
std::vector<Query> Q[N];

struct Edge {
  int v, nxt;
} e[N << 1];

inline void add(int u, int v) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
}

void dfs(int x) {
  for (int i = heads[x], v; i; i = e[i].nxt)
    if ((v = e[i].v) != f[0][x]) {
      dep[v] = dep[x] + 1;
      f[0][v] = x;
      dfs(v);
    }
}

inline int lca(int x, int y) {
  if (dep[x] < dep[y]) std::swap(x, y);
  for (int i = bit - 1; ~i; --i)
    if (dep[f[i][x]] >= dep[y]) x = f[i][x];
  if (x == y) return x;
  for (int i = bit - 1; ~i; --i)
    if (f[i][x] != f[i][y]) x = f[i][x], y = f[i][y];
  return f[0][x];
}

void dfs2(int x) {
  sum[t[x]]++;
  for (std::vector<Query>::iterator i = Q[x].begin(); i != Q[x].end(); ++i) {
    if (i->typ)
      ans[i->id] += sum[q[i->id]];
    else
      ans[i->id] -= (sum[q[i->id]] << 1) - ((t[x] == q[i->id]) ? 1 : 0);
  }
  for (int i = heads[x], v; i; i = e[i].nxt) {
    if ((v = e[i].v) != f[0][x]) {
      dfs2(v);
    }
  }
  sum[t[x]]--;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("CppTest.in", "r", stdin);
  freopen("CppTest.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &t[i]);
  for (int i = 1, x, y; i < n; ++i) scanf("%d%d", &x, &y), add(x, y), add(y, x);
  dep[1] = 1, dfs(1);
  for (int i = 1; i < bit; ++i)
    for (int j = 1; j <= n; ++j) f[i][j] = f[i - 1][f[i - 1][j]];
  for (int i = 1, a, b; i <= m; ++i) {
    scanf("%d%d%d", &a, &b, &q[i]);
    Q[a].push_back(Query(true, i));
    Q[b].push_back(Query(true, i));
    Q[lca(a, b)].push_back(Query(false, i));
  }
  dfs2(1);
  for (int i = 1; i <= m; ++i)
    if (ans[i] > 0)
      putchar('1');
    else
      putchar('0');
  return 0;
}